Imports Cdm.DataCore.CaseNotes

Partial Class DashboardControls_Notifications
    Inherits System.Web.UI.UserControl
    Implements IDashBoard

    Dim titleLabelString As String = ""
    Dim pageSizeValue As Integer = 0





    Public Property SortDirection() As System.Web.UI.WebControls.SortDirection Implements IDashBoard.SortDirection
        Get
            If ViewState("SortDirection") Is Nothing Then
                ViewState("SortDirection") = SortDirection.Ascending
            End If
            Return CType(ViewState("SortDirection"), SortDirection)
        End Get
        Set(ByVal value As System.Web.UI.WebControls.SortDirection)
            ViewState("SortDirection") = value
        End Set
    End Property

    Public Property PageNumber() As Integer Implements IDashBoard.PageNumber
        Get
            Return Me.NotificationsGridView.PageIndex
        End Get
        Set(ByVal value As Integer)
            Me.NotificationsGridView.PageIndex = value
        End Set
    End Property

    Public Property PageSize() As Integer Implements IDashBoard.PageSize
        Get
            If Not Me.NotificationsGridView Is Nothing Then
                If Me.NotificationsGridView.AllowPaging = False Then
                    Return -1
                Else
                    Return Me.NotificationsGridView.PageSize
                End If

            Else
                Return pageSizeValue
            End If
        End Get
        Set(ByVal value As Integer)
            If Not Me.NotificationsGridView Is Nothing Then
                If value = -1 Then
                    Me.NotificationsGridView.AllowPaging = False
                Else
                    Me.NotificationsGridView.AllowPaging = True
                    Me.NotificationsGridView.PageSize = value
                End If

                Me.NumberOfRowsDropDownList.SelectedValue = value.ToString()
            End If
            pageSizeValue = value
        End Set
    End Property

    Public Property SortField() As String Implements IDashBoard.SortField
        Get
            If ViewState("SortField") Is Nothing Then
                ViewState("SortField") = ""
            End If
            Return ViewState("SortField")
        End Get
        Set(ByVal value As String)
            ViewState("SortField") = value
        End Set
    End Property

    Public Property Title() As String Implements IDashBoard.Title
        Get
            If Me.TitleLabel IsNot Nothing Then
                Return TitleLabel.Text
            Else
                Return titleLabelString
            End If
        End Get
        Set(ByVal value As String)
            If TitleLabel IsNot Nothing Then
                TitleLabel.Text = value
            End If
            titleLabelString = value
        End Set
    End Property

    Protected Sub Page_Init(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Init

        AddHandler NumberOfRowsDropDownList.SelectedIndexChanged, AddressOf NumberOfRowsDropDownList_SelectedIndexChanged

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        If Not Me.IsPostBack Then
            Me.SortField = "DateGenerated"
            Me.SortDirection = WebControls.SortDirection.Descending
        End If

        'If Me.IsPostBack Then
        'AddHandler NumberOfRowsDropDownList.SelectedIndexChanged, AddressOf NumberOfRowsDropDownList_SelectedIndexChanged
        'Else
        'BindGrid()
        'End If

        If Not String.IsNullOrEmpty(titleLabelString) Then
            Me.TitleLabel.Text = titleLabelString
        End If

        If pageSizeValue > 0 Then
            Me.PageSize = pageSizeValue
        End If

        If Not Me.IsPostBack Then
            GridViewSettingController.SetGridViewValues(Me, 5, "DateGenerated", WebControls.SortDirection.Descending)
            Me.BindGrid()
        End If
    End Sub

    Public Sub SetScreenPositioning(ByVal displayRowsLeftPosition As Integer, _
                                    ByVal rightCornerLeftPosition As Integer, _
                                    ByVal headerWidth As Integer, _
                                    ByVal gridWidth As Integer)
        'UserControlTableWidth=rightcornerleft + 9
        'MiddleColumnTableData = UserControlTableWidth - 18
        'DisplaySpanTableData = displayRowsLeftPosition - 9)
        'DisplayRowsTableData = MiddleColumnTableData - DisplaySpanTableData

        Dim usercontrolwidth As Integer = rightCornerLeftPosition + 9
        Dim middlecolumnwidth As Integer = usercontrolwidth - 18
        Dim displayspantabledata As Integer = displayRowsLeftPosition - 9

        Me.HeaderDiv.Style("width") = usercontrolwidth.ToString() & "px"
        Me.UserControlTable.Style("width") = usercontrolwidth.ToString() & "px"
        Me.MiddleColumnTableData.Style("width") = middlecolumnwidth.ToString() & "px"
        Me.DisplaySpanTableData.Style("width") = (displayRowsLeftPosition - 9).ToString() & "px"
        Me.DisplayRowsTableData.Style("width") = (middlecolumnwidth - displayspantabledata).ToString() & "px"
        Me.GridViewTable.Style("width") = usercontrolwidth.ToString() & "px"
        Me.NotificationsGridView.Width = Unit.Pixel(usercontrolwidth)

    End Sub

    Protected Sub NumberOfRowsDropDownList_SelectedIndexChanged(ByVal sender As Object, _
                                                                ByVal e As System.EventArgs) _
                                                                Handles NumberOfRowsDropDownList.SelectedIndexChanged

        If NumberOfRowsDropDownList.SelectedValue = "-1" Then
            Me.NotificationsGridView.AllowPaging = False
        Else
            NotificationsGridView.AllowPaging = True
            NotificationsGridView.PageSize = NumberOfRowsDropDownList.SelectedValue
            NotificationsGridView.PageIndex = 0
           
            BindGrid()
        End If

    End Sub

    Public Sub BindGrid() Implements IDashBoard.BindGrid

        Me.NotificationsGridView.DataSource = Cdm.DataCore.CaseNotes.UserNotificationDisplay.ListByUser(Session("CurrentUserId"), SortField, Me.SortDirection.Equals(SortDirection.Ascending))
    
        Me.NotificationsGridView.PageIndex = Me.PageNumber
        'BindGrid()
        Me.NotificationsGridView.DataBind()
        GridViewSettingController.SaveSetting(Me)

    End Sub

    Protected Sub NotificationsGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotificationsGridView.DataBound
        AccessibilityController.AddImageToSortableGridHeaders(NotificationsGridView)
    End Sub

    Protected Sub NotificationsGridView_PageIndexChanging(ByVal sender As Object, _
                                                          ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) _
                                                          Handles NotificationsGridView.PageIndexChanging
        Me.PageNumber = e.NewPageIndex
        BindGrid()

    End Sub

    Public Sub LoadControl1(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Implements IDashBoard.LoadControl

        Me.Page_Load(sender, e)

    End Sub

    Protected Sub NotificationsGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles NotificationsGridView.RowDataBound
        Dim hyp As HyperLink = e.Row.FindControl("ViewFileHyperLink")
        Dim currentNotification As UserNotificationDisplay = e.Row.DataItem
        If Not hyp Is Nothing Then
            If currentNotification.NotificationId = Notification.NotificationType.NoteIsCreated Or _
               currentNotification.NotificationId = Notification.NotificationType.ParticipantHasMadeNote Or _
               currentNotification.NotificationId = Notification.NotificationType.ResponseIsCreated Then
                hyp.NavigateUrl = String.Format("~/Case/EditFile.aspx?participantid={0}&ActiveTab=Notes&unid={1}", currentNotification.ParticipantId, currentNotification.UserNotificationId)
            Else
                hyp.NavigateUrl = String.Format("~/Case/EditFile.aspx?participantid={0}", currentNotification.ParticipantId)
            End If

            'Do not show the view file link if this is a notification, 
            ' like Batch Processing complete, that is not associated with a participant.
            If currentNotification.ParticipantId > 0 Then
                hyp.Visible = True
            Else
                hyp.Visible = False
            End If

        End If

        Dim removeButton As Button = e.Row.FindControl("RemoveButton")
        If Not removeButton Is Nothing Then

            Dim deleteNotification As Cdm.DataCore.CaseNotes.UserNotificationDisplay = e.Row.DataItem
            Dim BLANK_MESSAGE As String
            Dim msg As String
            BLANK_MESSAGE = "Are you sure you want to remove the {0} notification for {1} entitled: \n {2}?"
            msg = String.Format(BLANK_MESSAGE, deleteNotification.DateGenerated.ToString(), _
            deleteNotification.FullName, deleteNotification.NotificationSubject)
            removeButton.Attributes.Add("onclick", String.Format("return confirm('{0}');", msg))
        End If

    End Sub

    Protected Sub Remove_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim removeButton As Button = CType(sender, Button)
        Dim id As Integer = CInt(removeButton.CommandArgument)
        Dim deleteNotification As Cdm.DataCore.UserSecurity.UserNotification
        deleteNotification = Cdm.DataCore.UserSecurity.UserNotification.Retrieve(id)
        deleteNotification.IsActive = False
        deleteNotification.Persist()
        BindGrid()
    End Sub

    Protected Sub NotificationsGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles NotificationsGridView.Sorting
        If e.SortExpression.Equals(Me.SortField) Then
            ToggleSortDirection()
        Else
            Me.SortDirection = WebControls.SortDirection.Ascending
        End If
        Me.SortField = e.SortExpression

        'Me.NotificationsGridView.SortExpression = e.SortExpression
        BindGrid()
    End Sub

    Private Sub ToggleSortDirection()
        If Me.SortDirection.Equals(SortDirection.Ascending) Then
            SortDirection = WebControls.SortDirection.Descending
        Else
            SortDirection = WebControls.SortDirection.Ascending
        End If
    End Sub
End Class
